package com.edu.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class loginFilter
 */
@WebFilter(urlPatterns= {"/index.jsp","/manage-users.jsp","/maps.jsp","/manage-Juan.jsp","/manage-Shi.jsp","/manage-Tu.jsp","/manage-Yuan.jsp"})
public class loginFilter implements Filter {

   
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
	
		HttpServletRequest hRequest=(HttpServletRequest)request;
		HttpServletResponse hresponse=(HttpServletResponse)response;
		HttpSession hSession=hRequest.getSession();
		String Identity="无";
		Cookie[] aCookie=hRequest.getCookies();
		
		//获取登录的身份，判断是否是null
		if(hSession.getAttribute("name")!=null) {
			Identity=(String)hSession.getAttribute("Identity");
		}
		String url=hRequest.getRequestURL().toString();
		
		//判断是否登录且是管理员
		if(aCookie.hashCode()>=2) {
				chain.doFilter(request, response);
				return;
		}
		
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated meth.getod stub
	}

}
